package com.kaizeli.website.util;

import com.kaizeli.website.exception.BusinessErrorEnum;
import com.kaizeli.website.exception.BusinessException;

import java.util.Objects;

/**
 * 断言工具类
 */
public class AsserterUtil {

    /**
     * 断言条件为真，否则抛出业务异常
     *
     * @param condition 布尔条件
     * @param BusinessErrorEnum 错误枚举
     * @param params    错误消息参数（可变参数）
     * @throws BusinessException 当条件为false时抛出业务异常
     */
    public static void isTrue(boolean condition, BusinessErrorEnum BusinessErrorEnum, Object... params) {
        if (!condition) {
            throw new BusinessException(BusinessErrorEnum, params);
        }
    }

    /**
     * 断言对象不为null，否则抛出业务异常
     *
     * @param object    要检查的对象
     * @param BusinessErrorEnum 错误枚举
     * @param params    错误消息参数（可变参数）
     * @throws BusinessException 当对象为null时抛出业务异常
     */
    public static void notNull(Object object, BusinessErrorEnum BusinessErrorEnum, Object... params) {
        if (object == null) {
            throw new BusinessException(BusinessErrorEnum, params);
        }
    }


    /**
     * 断言两个对象相等，否则抛出业务异常
     *
     * @param obj1      对象1
     * @param obj2      对象2
     * @param BusinessErrorEnum 错误枚举
     * @param params    错误消息参数（可变参数）
     * @throws BusinessException 当对象不相等时抛出业务异常
     */
    public static void equals(Object obj1, Object obj2, BusinessErrorEnum BusinessErrorEnum, Object... params) {
        if (!Objects.equals(obj1, obj2)) {
            throw new BusinessException(BusinessErrorEnum, params);
        }
    }
}